package study.exam.FileExam;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

public class FileExamActivity extends Activity implements View.OnClickListener{
    /** Called when the activity is first created. */
	private  File file;
	
	private EditText writeText;
	private EditText readText;
	private Button btnWrite;
	private Button btnRead; 

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
       // setContentView(R.layout.main);
        
		LinearLayout layout=new LinearLayout(this);
		layout.setBackgroundColor(Color.rgb(255,255,255));
		layout.setOrientation(LinearLayout.VERTICAL);
		setContentView(layout);
		

		writeText=new EditText(this);
		writeText.setText("",EditText.BufferType.NORMAL);
		setLLParams(writeText,240,70);
		layout.addView(writeText);	

		btnWrite=new Button(this);
		btnWrite.setText("쓰기");
		btnWrite.setOnClickListener(this);
		setLLParams(btnWrite);
		layout.addView(btnWrite);
		
		readText=new EditText(this);
		readText.setText("",EditText.BufferType.NORMAL);
		setLLParams(readText,240,70);
		layout.addView(readText);		

		btnRead=new Button(this);
		btnRead.setText("읽기");
		btnRead.setOnClickListener(this);
		setLLParams(btnRead);
		layout.addView(btnRead);	
     
        //String sdCardPath = Environment.getExternalStorageDirectory().getAbsolutePath();
        
		
        file = new File("/data/data/study.exam.FileExam/files/", "myFile");
        try {
			file.createNewFile();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
    
	public void onClick(View view) {
		
		if(view == btnWrite)
		{
	        try
	        {
	        	String strText = writeText.getText().toString();
	        	FileOutputStream fos = new FileOutputStream(file.getAbsoluteFile());
	        	fos.write(strText.getBytes());
	        	fos.close();
	        }
	        catch(IOException ie)
	        {
	        	ie.printStackTrace();
	        }			
		}
		else if(view == btnRead)
		{
	        if(file.exists())
	        {
	        	try
	        	{
	        		FileInputStream fis = new FileInputStream(file.getAbsoluteFile());
	        		long fileSizeInByte = file.length();
	        		byte[] bytes = new byte[(int)fileSizeInByte];
	        		fis.read(bytes);
	        		fis.close();
	        		
	        		
	        		readText.setText(new String(bytes));

	        	}
	        	catch(IOException ie)
	        	{
	        		ie.printStackTrace();
	        	}
	        }			
		}
		// TODO Auto-generated method stub
		
	}   
	private static void setLLParams(View view) {
		view.setLayoutParams(new LinearLayout.LayoutParams(
			LinearLayout.LayoutParams.WRAP_CONTENT,
			LinearLayout.LayoutParams.WRAP_CONTENT));
	}
	private static void setLLParams(View view,int w,int h) {
		view.setLayoutParams(new LinearLayout.LayoutParams(w,h));
	}	
    
}